package com.edu.exam.entity.assessment.generation;

import com.edu.exam.enums.assessment.generation.JobType;
import com.edu.exam.enums.assessment.generation.JobStatus;
import com.edu.exam.enums.assessment.generation.ExecutionMode;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 批量生成任务实体类
 * 所属模块：B-05 考核计划管理模块
 * 对应数据库表：batch_generation_job
 *
 * <p>批量生成考核任务的任务实体，记录批量生成的任务信息、
 * 执行状态、进度统计等。支持任务的创建、执行、监控、
 * 取消等完整生命周期管理。</p>
 *
 * @author 系统生成
 * @version 2.0
 * @since 2025-11-09
 */
@Entity
@Table(name = "batch_generation_job")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BatchGenerationJob {

    /**
     * 批次ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 批次编号
     */
    @Column(name = "batch_code", unique = true, length = 50, nullable = false)
    private String batchCode;

    /**
     * 批次名称
     */
    @Column(name = "batch_name", length = 200, nullable = false)
    private String batchName;

    /**
     * 批次描述
     */
    @Column(name = "batch_description", length = 1000)
    private String batchDescription;

    /**
     * 生成配置ID
     */
    @Column(name = "generation_config_id", nullable = false)
    private Long generationConfigId;

    /**
     * 生成配置名称
     */
    @Column(name = "generation_config_name", length = 100)
    private String generationConfigName;

    /**
     * 任务类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "job_type", length = 20, nullable = false)
    private JobType jobType;

    /**
     * 任务优先级
     */
    @Column(name = "priority", nullable = false)
    private Integer priority;

    /**
     * 任务状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "job_status", length = 20, nullable = false)
    private JobStatus jobStatus;

    /**
     * 执行模式
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "execution_mode", length = 20, nullable = false)
    private ExecutionMode executionMode;

    /**
     * 计划开始时间
     */
    @Column(name = "scheduled_start_time")
    private LocalDateTime scheduledStartTime;

    /**
     * 计划结束时间
     */
    @Column(name = "scheduled_end_time")
    private LocalDateTime scheduledEndTime;

    /**
     * 实际开始时间
     */
    @Column(name = "actual_start_time")
    private LocalDateTime actualStartTime;

    /**
     * 实际结束时间
     */
    @Column(name = "actual_end_time")
    private LocalDateTime actualEndTime;

    /**
     * 预计完成时间
     */
    @Column(name = "estimated_completion_time")
    private LocalDateTime estimatedCompletionTime;

    /**
     * 总任务数
     */
    @Column(name = "total_tasks")
    private Integer totalTasks;

    /**
     * 已完成任务数
     */
    @Column(name = "completed_tasks")
    private Integer completedTasks;

    /**
     * 失败任务数
     */
    @Column(name = "failed_tasks")
    private Integer failedTasks;

    /**
     * 跳过任务数
     */
    @Column(name = "skipped_tasks")
    private Integer skippedTasks;

    /**
     * 进度百分比
     */
    @Column(name = "progress_percentage")
    private Integer progressPercentage;

    /**
     * 执行参数（JSON格式）
     */
    @Column(name = "execution_parameters", columnDefinition = "TEXT")
    private String executionParameters;

    /**
     * 过滤条件（JSON格式）
     */
    @Column(name = "filter_criteria", columnDefinition = "TEXT")
    private String filterCriteria;

    /**
     * 生成规则（JSON格式）
     */
    @Column(name = "generation_rules", columnDefinition = "TEXT")
    private String generationRules;

    /**
     * 任务配置（JSON格式）
     */
    @Column(name = "task_configuration", columnDefinition = "TEXT")
    private String taskConfiguration;

    /**
     * 执行结果（JSON格式）
     */
    @Column(name = "execution_result", columnDefinition = "TEXT")
    private String executionResult;

    /**
     * 错误信息（JSON格式）
     */
    @Column(name = "error_details", columnDefinition = "TEXT")
    private String errorDetails;

    /**
     * 统计信息（JSON格式）
     */
    @Column(name = "statistics", columnDefinition = "TEXT")
    private String statistics;

    /**
     * 重试次数
     */
    @Column(name = "retry_count")
    private Integer retryCount;

    /**
     * 最大重试次数
     */
    @Column(name = "max_retry_count")
    private Integer maxRetryCount;

    /**
     * 超时时间（分钟）
     */
    @Column(name = "timeout_minutes")
    private Integer timeoutMinutes;

    /**
     * 是否允许重试
     */
    @Column(name = "allow_retry", nullable = false)
    private Boolean allowRetry;

    /**
     * 是否继续执行错误
     */
    @Column(name = "continue_on_error", nullable = false)
    private Boolean continueOnError;

    /**
     * 是否发送通知
     */
    @Column(name = "send_notification", nullable = false)
    private Boolean sendNotification;

    /**
     * 通知收件人（JSON格式）
     */
    @Column(name = "notification_recipients", columnDefinition = "TEXT")
    private String notificationRecipients;

    /**
     * 执行人ID
     */
    @Column(name = "executor_id", nullable = false)
    private Long executorId;

    /**
     * 执行人姓名
     */
    @Column(name = "executor_name", length = 100)
    private String executorName;

    /**
     * 删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 创建人ID
     */
    @Column(name = "created_by", nullable = false)
    private Long createdBy;

    /**
     * 创建人姓名
     */
    @Column(name = "created_by_name", length = 100)
    private String createdByName;

    /**
     * 更新人ID
     */
    @Column(name = "updated_by")
    private Long updatedBy;

    /**
     * 更新人姓名
     */
    @Column(name = "updated_by_name", length = 100)
    private String updatedByName;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 取消人ID
     */
    @Column(name = "cancelled_by")
    private Long cancelledBy;

    /**
     * 取消人姓名
     */
    @Column(name = "cancelled_by_name", length = 100)
    private String cancelledByName;

    /**
     * 取消时间
     */
    @Column(name = "cancelled_time")
    private LocalDateTime cancelledTime;

    /**
     * 取消原因
     */
    @Column(name = "cancellation_reason", length = 500)
    private String cancellationReason;

}